<?php
namespace app\controller;

use app\BaseController;
use app\model\User;
use think\Exception;
use think\facade\View;

class Index extends BaseController
{
    public function index()
    {
        return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V' . \think\facade\App::version() . '<br/><span style="font-size:30px;">16载初心不改 - 你值得信赖的PHP框架</span></p><span style="font-size:25px;">[ V6.0 版本由 <a href="https://www.yisu.com/" target="yisu">亿速云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ee9b1aa918103c4fc"></think>';
    }

    public function hello($name = 'ThinkPHP6')
    {
        return 'hello,' . $name;
    }
    public function info(){
        phpinfo();
    }

    public function test(){
        $serverName="27.115.111.58,14331";
        $user="sa";
        $password='Admin@123';
        $database='DSEDB';

        try {
            $connect=new \PDO("odbc:Driver={SQL Server};Server={$serverName};Database={$database}",$user,$password);
        }catch (\Exception $e){
            throw new Exception('连接数据库失败。'.$e->getMessage());
        }

        $query="select * from CONSLE_USER_TABLE_";
        $result=$connect->query($query);

        if($result){
            var_dump($result->fetchAll());
        }
        else{
            return '语句执行出错。'.$query;
        }
    }

    public function login(){
        if(request()->isAjax()){
            $username=trim(request()->param('username'));
            $password=trim(request()->param('password'));

            if(!$username){
                return error_json('请输入用户名');
            }
            if(!$password){
                return error_json('请输入密码');
            }

            try {
                $db=new User();
                $user=$db->findUser($username,md5($password));
            }catch (\Exception $e){
                return error_json($e->getMessage());
            }

            if(!$user){
                return error_json('用户名或密码错误');
            }
            return success_json('登陆成功',$user);
        }

        return View::fetch();
    }

    public function register(){
        if(request()->isAjax()){
            $username=trim(request()->param('username'));
            $password=trim(request()->param('password'));

            if(!$username){
                return error_json('请输入用户名');
            }
            if(!$password){
                return error_json('请输入密码');
            }

            try {
                $db=new User();
                $user=$db->findUser($username);
            }catch (\Exception $e){
                return error_json($e->getMessage());
            }
            if($user){
                return error_json('此用户名已被使用');
            }

            $result=$db->register($username,md5($password));

            return success_json('注册成功',[]);
        }

        return '不可访问';
    }

}
